package com.example.readbook.UI.Act

import android.os.Environment
import android.os.Handler
import android.support.v7.widget.LinearLayoutManager
import android.util.Log
import com.example.readbook.Adapter.BookScanningAdapter
import com.example.readbook.Bean.BookBean
import com.example.readbook.R
import com.example.readbook.Sql.SqliteUtils
import com.example.readbook.UI.BaseUI.BaseActivity
import com.example.readbook.Utils.Utils
import com.example.readbook.Utils.getNoLastName
import kotlinx.android.synthetic.main.act_book_scanning.*
import java.io.File
import java.util.*

/**
 * Package    : com.example.readbook.UI.Act
 * ClassName  : BookScanningActivity
 * Description: $书籍扫描
 * Date       : 2019/3/22 14:44
 */
class BookScanningActivity : BaseActivity() {
    val mFileList = mutableListOf<BookBean>()
    val mAdapter: BookScanningAdapter by lazy {
        BookScanningAdapter(mActivity, ArrayList<BookBean>())
    }
    val mHandler = Handler()

    override fun getLayoutResID(): Int {
        return R.layout.act_book_scanning
    }

    override fun initView() {
        title = "本地图书"
        Act_BookScann_Recy.layoutManager = LinearLayoutManager(mActivity, LinearLayoutManager.VERTICAL, false)
        Act_BookScann_Recy.adapter = mAdapter
    }

    override fun initData() {
        getBookList()
    }

    private fun getBookList() {
        loading.showD()
        loading.setText("正在获取")
        Thread {
            kotlin.run {
                val file = File(Environment.getExternalStorageDirectory().path)
                if (file.isDirectory) {
                    file.listFiles().forEach { genfile ->
                        if (genfile.isDirectory) {
                            genfile.listFiles().forEach { onefile ->
                                if (onefile.isDirectory) {
                                    onefile.listFiles().forEach { twofile ->
                                        if (!twofile.isDirectory) {
                                            if (getLast(twofile) || getLast(twofile)) {
                                                if (twofile.length() / 1024 > 2) {
                                                    mFileList.add(BookBean(twofile.getNoLastName(),
                                                            java.util.Random().nextInt(3).toString(), twofile.path,
                                                            Utils.m2((twofile.length() / 1024).toDouble(), 1).toString() + "K"))
                                                }
                                            }
                                            mHandler.removeCallbacks(mRunnable)
                                            mHandler.postDelayed(mRunnable, 1500)
                                        }
                                    }
                                } else {
                                    if (getLast(onefile) || getLast(onefile)) {
                                        if (onefile.length() / 1024 > 2) {
                                            mFileList.add(BookBean(onefile.getNoLastName(),
                                                    java.util.Random().nextInt(3).toString(), onefile.path,
                                                    Utils.m2((onefile.length() / 1024).toDouble(), 1).toString() + "K"))
                                        }
                                    }
                                    mHandler.removeCallbacks(mRunnable)
                                    mHandler.postDelayed(mRunnable, 1500)
                                }
                            }
                        } else {
                            if (getLast(genfile) || getLast(genfile)) {
                                if (genfile.length() / 1024 > 2) {
                                    mFileList.add(BookBean(genfile.getNoLastName(),
                                            java.util.Random().nextInt(3).toString(), genfile.path,
                                            Utils.m2((genfile.length() / 1024).toDouble(), 1).toString() + "K"))
                                }
                            }
                            mHandler.removeCallbacks(mRunnable)
                            mHandler.postDelayed(mRunnable, 1500)
                        }
                    }
                } else mHandler.post(mRunnable)
            }
        }.start()
    }

    private val mRunnable = Runnable {
        Log.e("yxs", "刷新次数")
        if (mFileList.isNotEmpty())
            mAdapter.updateList(mFileList)
        else
            Utils.showToast("没有符合要求的文件")
        loading.dismiss()
    }

    private fun getLast(file: File): Boolean {
        return file.name.let {
            if (it.length < 4)
                return false
            it.substring(it.length - 4, it.length) == ".txt"
                    || it.substring(it.length - 4, it.length) == ".TXT"
        }
    }

    override fun initListener() {
        Act_BookScann_ChooseAll.setOnClickListener {
            mAdapter.chooseAll(true)
            Log.e("yxs", "已选数组长度：" + mAdapter.mList.filter { it.isChoose }.size)

        }

        Act_BookScann_ChooseCancel.setOnClickListener {
            mAdapter.chooseAll(false)
            Log.e("yxs", "已选数组长度：" + mAdapter.mList.filter { it.isChoose }.size)
        }
        Act_BookScann_Add.setOnClickListener {
            loading.showD()
            loading.setText("正在添加0%")
            mAdapter.mList.filter {
                it.isChoose
            }.also {
                it.forEachIndexed { index, bookBean ->
                    Log.e("yxs", "循环：$index")
                    SqliteUtils.getInstence(mActivity).addBook(bookBean)
                    loading.setText("正在添加${index + 1 / it.size}%")
                    if (index + 1 == it.size) {
                        loading.dismiss()
                        setResult(9)
                        mAdapter.mList.clear()
                        mActivity.finish()
                    }
                }
            }


        }
    }
}